package com.mars.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.ticket.entity.SysUser;
import com.mars.ticket.request.SysNoticeRequest;
import com.mars.ticket.response.base.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mars.ticket.service.ISysNoticeService;
import com.mars.ticket.mapper.SysNoticeMapper;
import com.mars.ticket.entity.SysNotice;
import org.springframework.util.CollectionUtils;

import java.util.Date;


/**
 * SysNotice 实现层
 *
 * @Author wq
 * @Date 2021-05-20
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {


    @Override
    public PageInfo<SysNotice> list(SysNoticeRequest request) {
        IPage<SysNotice> page = new Page<>(request.getPageNumber(), request.getPageSize());

        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysNotice::getCreateTime);
        IPage<SysNotice> sysUserIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(sysUserIPage.getRecords())) {
            return new PageInfo<>(page.getTotal(), sysUserIPage.getRecords());
        }
        return null;
    }

    @Override
    public void add(SysNoticeRequest request) {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeTitle(request.getNoticeTitle());
        sysNotice.setNoticeContent(request.getNoticeContent());
        sysNotice.setNoticeType(2);
        sysNotice.setStatus(0);
        sysNotice.setCreateTime(new Date());
        sysNotice.setUpdateTime(new Date());
        save(sysNotice);
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public SysNotice detail(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void update(SysNoticeRequest request) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(request, sysNotice);
        updateById(sysNotice);
    }
}